/*
 * Copyright (C) 2017 WordPlat Open Source Project
 *
 *      https://wordplat.com/InteractiveKLineView/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wordplat.ikvstockchart;

import com.wordplat.ikvstockchart.compat.ViewUtils;
import com.wordplat.ikvstockchart.drawing.*;
import com.wordplat.ikvstockchart.entry.*;
import com.wordplat.ikvstockchart.marker.XAxisTextMarkerView;
import com.wordplat.ikvstockchart.marker.YAxisTextMarkerView;
import com.wordplat.ikvstockchart.render.KLineRender;

import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

/**
 * <p>InteractiveKLineLayout</p>
 * <p>Date: 2017/3/22</p>
 *
 * @author afon
 * @since 2017-03-22
 * @deprecated 这是一个含有股票技术指标的K线图示例，建议不要使用这个类用于真实项目中，此示例对如何编写自己的自定义布局提供参考。
 */

@Deprecated
public class InteractiveKLineLayout extends StackLayout implements Component.ClickedListener {

    private final Context context;

    private InteractiveKLineView kLineView;
    private KLineHandler kLineHandler;
    private KLineRender kLineRender;

    private StockMACDIndex macdIndex;
    private StockRSIIndex rsiIndex;
    private StockKDJIndex kdjIndex;
    private StockBOLLIndex bollIndex;

    private final int stockMarkerViewHeight;
    private final int stockIndexViewHeight;
    private final int stockIndexTabHeight;
    private RectFloat currentRect;

    private RadioButton MACD_But;
    private RadioButton RSI_But;
    private RadioButton KDJ_But;
    private RadioButton BOLL_But;
    private boolean isOnClickButton;

    /**
     * InteractiveKLineLayout
     *
     * @param context context
     * @param attrs   attrs
     */
    public InteractiveKLineLayout(Context context, AttrSet attrs) {
        this(context, attrs, 0);
    }

    /**
     * InteractiveKLineLayout
     *
     * @param context      context
     * @param attrs        attrs
     * @param defStyleAttr defStyleAttr
     */
    public InteractiveKLineLayout(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, "");
        this.context = context;
        stockMarkerViewHeight = ViewUtils.vpToPx(context, 15);
        stockIndexViewHeight = ViewUtils.vpToPx(context, 100);
        stockIndexTabHeight = ViewUtils.vpToPx(context, 20);
        initUI(context, attrs, defStyleAttr);
    }

    private void initUI(Context context, AttrSet attrs, int defStyleAttr) {
        kLineView = new InteractiveKLineView(context, attrs);
        kLineRender = (KLineRender) kLineView.getRender();

        kLineRender.setSizeColor(ViewUtils.getSizeColor(context, attrs, defStyleAttr));

        kLineView.setKLineHandler(new KLineHandler() {
            @Override
            public void onLeftRefresh() {
                if (kLineHandler != null) {
                    kLineHandler.onLeftRefresh();
                }
            }

            @Override
            public void onRightRefresh() {
                if (kLineHandler != null) {
                    kLineHandler.onRightRefresh();
                }
            }

            @Override
            public void onSingleTap(TouchEvent e, float x, float y) {
                if (kLineHandler != null) {
                    kLineHandler.onSingleTap(e, x, y);
                }
                onTabClick(x, y);
            }

            @Override
            public void onDoubleTap(TouchEvent e, float x, float y) {
                if (kLineHandler != null) {
                    kLineHandler.onDoubleTap(e, x, y);
                }
            }

            @Override
            public void onHighlight(Entry entry, int entryIndex, float x, float y) {
                if (kLineHandler != null) {
                    kLineHandler.onHighlight(entry, entryIndex, x, y);
                }
            }

            @Override
            public void onCancelHighlight() {
                if (kLineHandler != null) {
                    kLineHandler.onCancelHighlight();
                }
            }
        });

        /**
         * 成交量
         */
        StockKLineVolumeIndex kLineVolumeIndex = new StockKLineVolumeIndex(stockIndexViewHeight);
        kLineVolumeIndex.addDrawing(new KLineVolumeDrawing());
        kLineVolumeIndex.addDrawing(new KLineVolumeHighlightDrawing());
        kLineRender.addStockIndex(kLineVolumeIndex);

        /**
         * MACD
         */
        HighlightDrawing macdHighlightDrawing = new HighlightDrawing();
        macdHighlightDrawing.addMarkerView(new YAxisTextMarkerView(stockMarkerViewHeight));

        macdIndex = new StockMACDIndex(stockIndexViewHeight);
        macdIndex.addDrawing(new MACDDrawing());
        macdIndex.addDrawing(new StockIndexYLabelDrawing());
        macdIndex.addDrawing(macdHighlightDrawing);
        macdIndex.setPaddingTop(stockIndexTabHeight);
        kLineRender.addStockIndex(macdIndex);

        /**
         * RSI
         */
        HighlightDrawing rsiHighlightDrawing = new HighlightDrawing();
        rsiHighlightDrawing.addMarkerView(new YAxisTextMarkerView(stockMarkerViewHeight));

        rsiIndex = new StockRSIIndex(stockIndexViewHeight);
        rsiIndex.addDrawing(new RSIDrawing());
        rsiIndex.addDrawing(new StockIndexYLabelDrawing());
        rsiIndex.addDrawing(rsiHighlightDrawing);
        rsiIndex.setPaddingTop(stockIndexTabHeight);
        kLineRender.addStockIndex(rsiIndex);

        /**
         * KDJ
         */
        HighlightDrawing kdjHighlightDrawing = new HighlightDrawing();
        kdjHighlightDrawing.addMarkerView(new YAxisTextMarkerView(stockMarkerViewHeight));

        kdjIndex = new StockKDJIndex(stockIndexViewHeight);
        kdjIndex.addDrawing(new KDJDrawing());
        kdjIndex.addDrawing(new StockIndexYLabelDrawing());
        kdjIndex.addDrawing(kdjHighlightDrawing);
        kdjIndex.setPaddingTop(stockIndexTabHeight);
        kLineRender.addStockIndex(kdjIndex);

        /**
         * BOLL
         */
        HighlightDrawing bollHighlightDrawing = new HighlightDrawing();
        bollHighlightDrawing.addMarkerView(new YAxisTextMarkerView(stockMarkerViewHeight));

        bollIndex = new StockBOLLIndex(stockIndexViewHeight);
        bollIndex.addDrawing(new BOLLDrawing());
        bollIndex.addDrawing(new StockIndexYLabelDrawing());
        bollIndex.addDrawing(bollHighlightDrawing);
        bollIndex.setPaddingTop(stockIndexTabHeight);
        kLineRender.addStockIndex(bollIndex);

        kLineRender.addMarkerView(new YAxisTextMarkerView(stockMarkerViewHeight));
        kLineRender.addMarkerView(new XAxisTextMarkerView(stockMarkerViewHeight));

        addComponent(kLineView);
    }

    /**
     * setClick
     *
     * @param but_Group
     * @param MACD_But
     * @param RSI_But
     * @param KDJ_But
     * @param BOLL_But
     */
    public void setClick(RadioContainer but_Group, RadioButton MACD_But, RadioButton RSI_But, RadioButton KDJ_But, RadioButton BOLL_But) {
        this.MACD_But = MACD_But;
        this.RSI_But = RSI_But;
        this.KDJ_But = KDJ_But;
        this.BOLL_But = BOLL_But;

        MACD_But.setClickedListener(this);
        RSI_But.setClickedListener(this);
        KDJ_But.setClickedListener(this);
        BOLL_But.setClickedListener(this);

        kLineView.postLayout();

        showMACD();
    }

    public InteractiveKLineView getKLineView() {
        return kLineView;
    }

    /**
     * setKLineHandler
     *
     * @param kLineHandler
     */
    public void setKLineHandler(KLineHandler kLineHandler) {
        this.kLineHandler = kLineHandler;
    }

    private void onTabClick(float x, float y) {
        if (isOnClickButton) {
            isOnClickButton = false;
            return;
        }
        if (currentRect == null) {
            return;
        }
        if (currentRect.isInclude(x, y)) {
            if (macdIndex.isEnable()) {
                showRSI();
            } else if (rsiIndex.isEnable()) {
                showKDJ();
            } else if (kdjIndex.isEnable()) {
                showBOLL();
            } else {
                showMACD();
            }

            if (kLineHandler != null) {
                kLineHandler.onCancelHighlight();
            }

            kLineView.notifyDataSetChanged();
        }
    }

    /**
     * showMACD
     */
    public void showMACD() {
        macdIndex.setEnable(true);
        rsiIndex.setEnable(false);
        kdjIndex.setEnable(false);
        bollIndex.setEnable(false);

        MACD_But.setChecked(true);
        RSI_But.setChecked(false);
        KDJ_But.setChecked(false);
        BOLL_But.setChecked(false);

        MACD_But.setTextColor(new Color(Color.rgb(30, 130, 210)));
        RSI_But.setTextColor(new Color(Color.rgb(136, 136, 136)));
        KDJ_But.setTextColor(new Color(Color.rgb(136, 136, 136)));
        BOLL_But.setTextColor(new Color(Color.rgb(136, 136, 136)));

        currentRect = macdIndex.getRect();
    }

    /**
     * showRSI
     */
    public void showRSI() {
        macdIndex.setEnable(false);
        rsiIndex.setEnable(true);
        kdjIndex.setEnable(false);
        bollIndex.setEnable(false);

        MACD_But.setChecked(false);
        RSI_But.setChecked(true);
        KDJ_But.setChecked(false);
        BOLL_But.setChecked(false);

        MACD_But.setTextColor(new Color(Color.rgb(136, 136, 136)));
        RSI_But.setTextColor(new Color(Color.rgb(30, 130, 210)));
        KDJ_But.setTextColor(new Color(Color.rgb(136, 136, 136)));
        BOLL_But.setTextColor(new Color(Color.rgb(136, 136, 136)));

        currentRect = rsiIndex.getRect();
    }

    /**
     * showKDJ
     */
    public void showKDJ() {
        macdIndex.setEnable(false);
        rsiIndex.setEnable(false);
        kdjIndex.setEnable(true);
        bollIndex.setEnable(false);

        MACD_But.setChecked(false);
        RSI_But.setChecked(false);
        KDJ_But.setChecked(true);
        BOLL_But.setChecked(false);

        MACD_But.setTextColor(new Color(Color.rgb(136, 136, 136)));
        RSI_But.setTextColor(new Color(Color.rgb(136, 136, 136)));
        KDJ_But.setTextColor(new Color(Color.rgb(30, 130, 210)));
        BOLL_But.setTextColor(new Color(Color.rgb(136, 136, 136)));

        currentRect = kdjIndex.getRect();
    }

    /**
     * showBOLL
     */
    public void showBOLL() {
        macdIndex.setEnable(false);
        rsiIndex.setEnable(false);
        kdjIndex.setEnable(false);
        bollIndex.setEnable(true);

        MACD_But.setChecked(false);
        RSI_But.setChecked(false);
        KDJ_But.setChecked(false);
        BOLL_But.setChecked(true);

        MACD_But.setTextColor(new Color(Color.rgb(136, 136, 136)));
        RSI_But.setTextColor(new Color(Color.rgb(136, 136, 136)));
        KDJ_But.setTextColor(new Color(Color.rgb(136, 136, 136)));
        BOLL_But.setTextColor(new Color(Color.rgb(30, 130, 210)));

        currentRect = bollIndex.getRect();
    }

    /**
     * isShownMACD
     *
     * @return isShownMACD
     */
    public boolean isShownMACD() {
        return macdIndex.isEnable();
    }

    /**
     * isShownRSI
     *
     * @return isShownRSI
     */
    public boolean isShownRSI() {
        return rsiIndex.isEnable();
    }

    /**
     * isShownKDJ
     *
     * @return isShownKDJ
     */
    public boolean isShownKDJ() {
        return kdjIndex.isEnable();
    }

    /**
     * isShownBOLL
     *
     * @return isShownBOLL
     */
    public boolean isShownBOLL() {
        return bollIndex.isEnable();
    }

    @Override
    public void onClick(Component component) {
        final int id = component.getId();
        isOnClickButton = true;
        if (id == ResourceTable.Id_MACD_But) {
            showMACD();
        } else if (id == ResourceTable.Id_RSI_But) {
            showRSI();
        } else if (id == ResourceTable.Id_KDJ_But) {
            showKDJ();
        } else if (id == ResourceTable.Id_BOLL_But) {
            showBOLL();
        }

        if (kLineHandler != null) {
            kLineHandler.onCancelHighlight();
        }

        kLineView.notifyDataSetChanged();
    }
}
